window.onload = function() {
    var register = document.getElementsByClassName("register")[0];
    register.onclick = function() {
        window.location.href = "register.html"
    }
    var givein = document.getElementById("givein");
    var uservalue = document.getElementById("user")
    var forgetMima = document.getElementsByClassName("forgetMima")[0];
    forgetMima.onmouseover = function() {
        this.style.color = "orange"
    }
    forgetMima.onmouseout = function() {
        this.style.color = "#fff"
    }
    var yX = document.getElementsByClassName("yX")[0];
    yX.onmouseover = function() {
        this.style.color = "orange"
    }
    yX.onmouseout = function() {
        this.style.color = "#fff"
    }
    var youxiangDenglu = document.getElementsByClassName("youxiangDenglu")[0];
    var remove = document.getElementById("remove");
    var yX = document.getElementsByClassName("yX")[0];
    // const { default: axios } = require("axios");


    var register = document.getElementsByClassName("register")[0];
    register.onclick = function() {
        window.location.href = "register.html"
    }
    var givein = document.getElementById("givein");
    var uservalue = document.getElementById("user")
    var forgetMima = document.getElementsByClassName("forgetMima")[0];
    forgetMima.onmouseover = function() {
        this.style.color = "orange"
    }
    forgetMima.onmouseout = function() {
        this.style.color = "#fff"
    }
    var yX = document.getElementsByClassName("yX")[0];
    yX.onmouseover = function() {
        this.style.color = "orange"
    }
    yX.onmouseout = function() {
        this.style.color = "#fff"
    }
    var youxiangDenglu = document.getElementsByClassName("youxiangDenglu")[0];
    var remove = document.getElementById("remove");
    var yX = document.getElementsByClassName("yX")[0];

    yX.onclick = () => {
        SrcCodd()
        youxiangDenglu.style.display = "block"
    }
    remove.onclick = function() {
        SrcCod()
        youxiangDenglu.style.display = "none"

    }

    var zhaohuimima = document.getElementsByClassName('zhaohuimima')[0]
    var removee = document.getElementById('removee')
    var forgetMima = document.getElementsByClassName('forgetMima')[0]

    forgetMima.onclick = () => {
        zhaohuimima.style.display = 'block'
    }


    removee.onclick = function() {
        zhaohuimima.style.display = 'none'
    }

    //获取设置新密码表单，点击×，表单不显示
    var newpassword = document.getElementById('newpassword')
    var removeee = document.getElementById('removeee')

    removeee.onclick = function() {
            newpassword.style.display = 'none'
        }

        //正常登录功能
    let zhanghao = document.getElementById("zhanghao")
    let mima = document.getElementById("mima")
    let gives = document.getElementById("gives")
    var leftIput = document.getElementById('leftInput');
    gives.onclick = function() {
        loginin()
    }
//封装正常登录函数
    function loginin(){
        axios({
            method: 'POST',
            url: '/user/login',
            params: {
                zhanghao: zhanghao.value,
                mima: mima.value,
                code: leftIput.value
            }
        }).then(data => {
            console.log(zhanghao.value);
            console.log(mima.value);
            console.log(data);
            console.log(data.data.data.code == 20000);
            if (data.data.data.code == 20000) {
                tigs(data.data.data.message, true)
                   
                localStorage.setItem('user', data.data.id)
                setTimeout(() => {
                    window.location.href = '/picture/homePage'
                }, 1000)
            } else {
                tigs(data.data.data.message)
            }
        }).catch(err => {

            console.log(err);

        })}

    //邮箱找回密码
    var email = document.getElementById('email')
    var go = document.getElementById('go')
    var getCode = document.getElementById('getCode')
    var youxiangMa = document.getElementById('youxiangMa')
    var newpassword = document.getElementsByClassName('newpassword')[0]



    getCode.onclick = function() {
        axios({
            method: 'GET',
            url: '/getCode',
            params: {
                email: email.value
            }
        }).then(data => {
            console.log(data.data.data);
            if (!email.value) {
                alert('请输入邮箱！')
                clearInterval(timer)
                getCode.disabled = false; //按钮解禁
                getCode.innerHTML = '获取验证码';
            } else if (data.data.data.message === '成功') {
                alert('验证码发送成功!')
                    //点击获取验证码后按钮禁用
                getCode.disabled = true;
                var time = 3;
                //设置定时器timer
                var timer = setInterval(
                    function() {
                        if (time == 0) {
                            clearInterval(timer) //清除定时器
                            getCode.disabled = false; //按钮解禁
                            getCode.innerHTML = '获取验证码';
                            time = 3 //重新发送仍从30开始
                        } else {
                            getCode.innerHTML = '重新发送' + time + '秒';
                            time--; //按钮里的数值自减
                        }
                    }, 1000); //忘记密码，判断判断邮箱验证码是否与输入框内容相同
                go.onclick = function() {
                    axios({
                        method: 'get',
                        url: '/examinecode',
                        params: {
                            email: email.value,
                            code: youxiangMa.value
                        }
                    }).then(data => {
                        if (data.data.data.data === '邮箱验证成功') {
                            alert('验证码正确！')
                            newpassword.style.display = 'block'
                                //用户设置新密码
                            var passwordone = document.getElementById('passwordone')
                            var passwordtwo = document.getElementById('passwordtwo')
                            var determine = document.getElementById('determine')

                            determine.onclick = function() {
                                axios({
                                    method: 'POST',
                                    url: '/setnewpassword',
                                    params: {
                                        email: email.value,
                                        password: passwordone.value,
                                        repeatpassword: passwordtwo.value
                                    }
                                }).then(data => {
                                    if (!passwordone.value || !passwordtwo.value) {
                                        alert('密码不能为空，请输入密码!')
                                    } else if (data.data.data.message === '密码不能为空，且密码长度在6~16位之间') {
                                        alert('请输入6~16位的新密码！')
                                    } else {
                                        alert('新密码设置成功!')
                                        console.log(('zhaohuimima'));
                                        newpassword.style.display = 'none'
                                        zhaohuimima.style.display = 'none'

                                    }

                                }).catch(err => {
                                        console.log(err);
                                        alert('no')
                                    }

                                )
                            }
                        } else {
                            alert('请输入正确邮箱验证码！')

                        }

                    }).catch(err => {
                        console.log(err);

                    })
                }

            } else if (data.data.data.message === '验证码发送失败') {
                alert('验证码发送失败！')
            } else if (data.data.data.message === '该邮箱已被注册!') {
                alert('改邮箱已被注册！请继续... ')

            } else if (data.data.data.message === '错误的邮箱格式') {
                alert('请输入正确的邮箱格式!')
            } else {
                alert('该邮箱没有绑定，请进行绑定!')
            }
        }).catch(err => {
            console.log(err);
        })


    }




    //邮箱登录
    var emaillogingo = document.getElementById('emaillogingo')
    var haa = document.getElementById('haa')
    var hbb = document.getElementById('hbb')
    var emailLeft = document.getElementById('emailLeft')



    emaillogingo.onclick = function() {

            axios({
                method: 'POST',
                url: '/email/login',
                prams: {
                    code: emailLeft.value,
                    email: haa.value,
                    password: hbb.value
                }
            }).then(data => {
                console.log(data);
                if (!emailLeft.value || !haa.value || !hbb.value) {
                    alert('输入信息不能为空！')
                } else if (data.data.data.message === '请输入正确的验证码') {
                    alert('请输入正确的验证码！')
                } else {
                    alert('111！')

                }

            }).catch(err => {
                console.log(err);
                alert('no')
            })

        }
        //用户退出登录

    function logoff() {
        axios({
            method: 'POST',
            url: '/email/login',
            prams: {
                code: emailLeft.value,
                email: haa.value,
                password: hbb.value
            }
        }).then(data => {
            console.log(data);
            if (!emailLeft.value || !haa.value || !hbb.value) {
                alert('输入信息不能为空！')
            } else if (data.data.data.message === '请输入正确的验证码') {
                alert('请输入正确的验证码！')
            } else {
                alert('111！')

            }

        }).catch(err => {
            console.log(err);
            alert('no')
        })


    }

















    //验证码更新
    let srcCode = document.getElementsByClassName('srcCode')[0]
    srcCode.onclick = function() {
        SrcCod()
    }

    function SrcCod() {
        axios.get('/user/code').then((data) => {
            // console.log(data);
            let message = data.data.data.img
                // console.log(message);
            srcCode.setAttribute('src', message);
        })
    }
    SrcCod()

    // 验证码更新(邮箱登录)
    let srcCodes = document.getElementsByClassName('srcCodes')[0]
    srcCodes.onclick = function() {
        SrcCodd()
    }

    function SrcCodd() {
        axios.get('/user/code').then((data) => {
            console.log(data);
            let message = data.data.data.img
            console.log(message);
            srcCodes.setAttribute('src', message);
        })
    }

}